<?php

class Application_Model_TestQuestionnaireMapper
{

    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_TestQuestionnaire');
        }
        return $this->_dbTable;
    }

    public function save(Application_Model_TestQuestionnaire $TestQuestionnaire)
    {
        $data = array(
            'test_id'   => $TestQuestionnaire->getTestId(),
            'questionnaire_id'   => $TestQuestionnaire->getQuestionnaireId(),
            'id'   => $TestQuestionnaire->getId(),

        );

        if (null === ($id = $TestQuestionnaire->getId())) {
            unset($data['id']);
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('id = ?' => $id));
        }
    }

    public function delete(Application_Model_TestQuestionnaire $TestQuestionnaire)
    {
        $data = array(
            'test_id'   => $TestQuestionnaire->getTestId(),
            'questionnaire_id'   => $TestQuestionnaire->getQuestionnaireId(),
            'id' => $TestQuestionnaire->getId(),
        );

        $id = $TestQuestionnaire->getId();

        $this->getDbTable()->delete(array('id = ?' => $id));
    }

    public function find($id, Application_Model_TestQuestionnaire $TestQuestionnaire)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $row = $result->current();
        $TestQuestionnaire->setId($row->id)
                  ->setTestId($row->test_id)
                  ->setQuestionnaireId($row->questionnaire_id);
    }

    public function fetchAll()
    {
        $resultSet = $this->getDbTable()->fetchAll();
        $entries   = array();
        foreach ($resultSet as $row) {
            $entry = new Application_Model_TestQuestionnaire();
            $entry->setId($row->id)
                  ->setTestId($row->test_id)
                  ->setQuestionnaireId($row->questionnaire_id);
            $entries[] = $entry;
        }
        return $entries;
    }

    public function fetchByQuestionnaireTest($q_id, $test_id) {
        /**
                     *  @return the first row that matches with questionnaire and test
                     */
        
        $table = new Application_Model_DbTable_TestQuestionnaire();
        $select = $table->select();
        $select->from($table)->where('test_id = ?', $test_id)
                ->where('questionnaire_id = ?', $q_id);

        $resultSet = $table->fetchAll($select);

        //$resultSet = $this->getDbTable()->fetchAll();

        if (count($resultSet) == 0) {
            return null;
        } else {
            return $resultSet[0];
        }
        
    }
}

